const printDate=()=> {
				const d = new Date();
				const year = d.getFullYear();
				const month = d.getMonth() > 8 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1);
				const date = d.getDate() > 9 ? d.getDate() : "0" + d.getDate();
				const hours = d.getHours() > 9 ? d.getHours() : "0" + d.getHours();
				const minutes = d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes();
				const seconds = d.getSeconds() > 9 ? d.getSeconds() : "0" + d.getSeconds();
				return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
			}
export function comjs(app) {

	app.config.globalProperties.getday = (dateTimeStamp) => {
		
		 var timestamp = new Date(dateTimeStamp.replace(/-/g, '/')).getTime();
		 var minute = 1000 * 60;
		 var hour = minute * 60;
		 var day = hour * 24;
		 var halfamonth = day * 15;
		 var month = day * 30;
		 var year = day * 365;
		 			
		 var now = new Date(printDate()).getTime();
		 			
		 var diffValue = now - timestamp;
		 var result;
		 if (diffValue < 0) {
		 	return;
		 }
		 var yearC = diffValue / year;
		 var monthC = diffValue / month;
		 var weekC = diffValue / (7 * day);
		 var dayC = diffValue / day;
		 var hourC = diffValue / hour;
		 var minC = diffValue / minute;
		 if (yearC >= 1) {
		 	result = "" + parseInt(yearC) + "年前";
		 } else if (monthC >= 1) {
		 	result = "" + parseInt(monthC) + "月前";
		 } else if (weekC >= 1) {
		 	result = "" + parseInt(weekC) + "周前";
		 } else if (dayC >= 1) {
		 	result = "" + parseInt(dayC) + "天前";
		 } else if (hourC >= 1) {
		 	result = "" + parseInt(hourC) + "小时前";
		 } else if (minC >= 1) {
		 	result = "" + parseInt(minC) + "分钟前";
		 } else
		 	result = "刚刚";
		 return result;
		
	}

	// app.config.globalProperties.getday = (d) => {
		
	// 	d = d.replace(/-/g, '/')
	// 	var td = new Date();
	// 	td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
	// 	var od = new Date(d);
	// 	od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
	// 	var xc = (od - td) / 1000 / 60 / 60 / 24;
		
		
	// 	if (xc < -1) {
	// 		return "前天"+d.split(' ')[1].substring(0,5)
	// 	} else if (xc < 0) {
	// 		return "昨天"+d.split(' ')[1].substring(0,5)
	// 	} else if (xc == 0) {
	// 		return "今天" + d.split(' ')[1].substring(0,5);
	// 	} else {
	// 		return d
	// 	}
	// }


	app.config.globalProperties.newdate = (dateTimeStamp) => {
		const d = new Date();
		const ayear = d.getFullYear();
		const amonth = d.getMonth() > 8 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1);
		const date = d.getDate() > 9 ? d.getDate() : "0" + d.getDate();
		const hours = d.getHours() > 9 ? d.getHours() : "0" + d.getHours();
		const minutes = d.getMinutes() > 9 ? d.getMinutes() : "0" + d.getMinutes();
		const seconds = d.getSeconds() > 9 ? d.getSeconds() : "0" + d.getSeconds();
		var dtime = `${ayear}-${amonth}-${date} ${hours}:${minutes}:${seconds}`

		if (!dateTimeStamp) {
			return
		}
		var timestamp = new Date(dateTimeStamp.replace(/-/g, '/')).getTime();
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var year = day * 365;

		var now = new Date(dtime).getTime();

		var diffValue = now - timestamp;
		var result;
		if (diffValue < 0) {
			return;
		}
		var yearC = diffValue / year;
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var minC = diffValue / minute;
		if (yearC >= 1) {
			result = "" + parseInt(yearC) + "年前";
		} else if (monthC >= 1) {
			result = "" + parseInt(monthC) + "月前";
		} else if (weekC >= 1) {
			result = "" + parseInt(weekC) + "周前";
		} else if (dayC >= 1) {
			result = "" + parseInt(dayC) + "天前";
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前";
		} else if (minC >= 1) {
			result = "" + parseInt(minC) + "分钟前";
		} else
			result = "刚刚";
		return result;
	}




	app.config.globalProperties.shh = uni.getSystemInfoSync().statusBarHeight
	app.config.globalProperties.navto = (url) => {
		uni.navigateTo({
			url
		})
	}

	app.config.globalProperties.copy = (copy) => {
		uni.setClipboardData({
			data: copy,
			icon: "none",
			success: function() {
				console.log('success');
			}
		})
	}

	app.config.globalProperties.back = () => {
		if (getCurrentPages().length == 1) {
			uni.reLaunch({
				url: "/pages/index/index"
			})
		} else {
			uni.navigateBack()
		}
	}
	
	app.config.globalProperties.tabBadge = () => {
		var arrmsg = uni.getStorageSync('msg')
		var tabindex = 0
		if(arrmsg){
			arrmsg.map(item=>{
				if(item.unid == uni.getStorageSync('user').uid){
					item.list.map(items=>{
						tabindex +=items.allunread
					})
				}
			})
			return tabindex
		}
		
		// if(tabindex){
		// 	return 
		// 	// uni.setStorageSync('tabindex',tabindex)
		// 	// uni.setTabBarBadge({
		// 	//   index: 2,
		// 	//   text: tabindex
		// 	// })
		// }
	}

	app.config.globalProperties.upimg = (count) => {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: count ? count : 9,
				success: (res) => {
					uni.showLoading({
						mask: true
					})
					var image = res.tempFilePaths
					var ff = []
					for (let i = 0; i < image.length; i++) {
						uni.uploadFile({
							url: app.config.globalProperties.apiurl +
								'/api/upload/image',
							fileType: 'image',
							name: 'pics',
							filePath: image[i],
							formData: {
								filename: "pics",
							},
							header: {
								'Authori-zation': 'Bearer ' + uni.getStorageSync(
									"token"),
							},
							success: (uploadFileRes) => {
								var ddt = JSON.parse(uploadFileRes.data)
								if (ddt.status == 200) {
									ff.push(ddt.data.url)
									if (ff.length == image.length) {
										resolve(ff)
										uni.hideLoading()
									}
								} else {
									uni.showModal({
										content: ddt.msg
									})
									uni.hideLoading()
								}
							},
							fail(e) {
								uni.hideLoading()
								uni.showModal({
									content: "网络错误,稍后再试"
								})
							}
						})
					}
				},
			})
		})
	}
}